//
//  CoreDataMan.m
//  Eyess
//
//  Created by lanou on 15/11/18.
//  Copyright © 2015年 追随我心. All rights reserved.
//

#import "CoreDataMan.h"

#import <CoreData/CoreData.h>
@interface CoreDataMan ()
@property (nonatomic, strong) NSManagedObjectContext *context;
@end
@implementation CoreDataMan
- (instancetype)init
{
    if (self = [super init])
    {
        self.context  = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        NSString *string = [[NSBundle mainBundle]pathForResource:@"MyLike" ofType:@"momd"];
        NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath: string]];
        NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
        
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
        
        NSString *sqlite = [path stringByAppendingPathComponent:@"Like.sqlite"];
        //NSLog(@"收藏数据库%@",sqlite);
        [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlite] options:nil error:nil];
        self.context.persistentStoreCoordinator = store;
        
        
    }
    return self;
}
+(CoreDataMan *)shareIntance
{
    static CoreDataMan *man = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        man = [[CoreDataMan alloc] init];
    });
    return man;
}


/**
 *  添加播放收藏
 *
 *  @param playUrl 播放url
 *  @param imagUrl 图片
 *  @param title   标题或者描述
 */
- (void)addPlay:(NSString *)playUrl imagUrl:(NSString *)imagUrl title:(NSString *)title width:(NSInteger)width height:(NSInteger)height
{
    Play *aplay = [NSEntityDescription insertNewObjectForEntityForName:@"Play" inManagedObjectContext:self.context];
    aplay.playUrl = playUrl;
    aplay.imagUrl = imagUrl;
    aplay.title = title;
    aplay.width = [NSNumber numberWithInteger:width];
    aplay.height = [NSNumber numberWithInteger:height];
    
    [self.context save:nil];
    
}

/**
 *  添加每日一晒
 *
 *  @param p_j_id  拼接id
 *  @param imagUrl 图片url
 *  @param title   标题或者描述
 */
- (void)addEvery:(NSString *)p_j_id imageUrl:(NSString *)imagUrl title:(NSString *)title width:(NSInteger)width height:(NSInteger)height
{
    Every *aEvery = [NSEntityDescription insertNewObjectForEntityForName:@"Every" inManagedObjectContext:self.context];
    aEvery.p_j_id = p_j_id;
    aEvery.imagUrl = imagUrl;
    aEvery.title = title;
    aEvery.width = [NSNumber numberWithInteger:width];
    aEvery.height = [NSNumber numberWithInteger:height];
    [self.context save:nil];
}

/**
 *  添加购物单品
 *
 *  @param P_j_id   拼接id
 *  @param imageUrl 图片url
 *  @param title    标题或者描述
 *  @param type_id  拼接id
 */
- (void)addSku:(NSString *)P_j_id imageUrl:(NSString *)imageUrl title:(NSString *)title type_id:(NSString *)type_id width:(NSInteger)width height:(NSInteger)height
{
    Sku *aSku = [NSEntityDescription insertNewObjectForEntityForName:@"Sku" inManagedObjectContext:self.context];
    aSku.p_j_id = P_j_id;
    aSku.imageUrl = imageUrl;
    aSku.title = title;
    aSku.type_id = type_id;
    aSku.width = [NSNumber numberWithInteger:width];
    aSku.height = [NSNumber numberWithInteger:height];
    [self.context save:nil];
    
}

/**
 *  取消播放收藏
 *
 *  @param playUrl 播放url
 */
- (void)deletePlayWithId:(NSString *)playUrl
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Play"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"playUrl = %@",playUrl];
    request.predicate = predicate;
    
    Play *play = [self.context executeFetchRequest:request error:nil].firstObject ;
    [self.context deleteObject:play];
    [self.context save:nil];
}

/**
 *  取消单品收藏
 *
 *  @param P_j_id 拼接id
 */
- (void)deleteSkuWithId:(NSString *)P_j_id
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Sku"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"p_j_id = %@",P_j_id];
    request.predicate = predicate;
    
    Sku *play = [self.context executeFetchRequest:request error:nil].firstObject ;
    [self.context deleteObject:play];
    [self.context save:nil];

}

/**
 *  取消每日一晒
 *
 *  @param P_j_id 拼接id
 */
- (void)deleteEveryWithId:(NSString *)P_j_id
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Every"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"p_j_id = %@",P_j_id];
    request.predicate = predicate;
    
    Play *play = [self.context executeFetchRequest:request error:nil].firstObject ;
    [self.context deleteObject:play];
    [self.context save:nil];

}

- (BOOL)findPlayWithId:(NSString *)playUrl
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Play"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"playUrl = %@",playUrl];
    request.predicate = predicate;
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    if (array.count == 0)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

/**
 *  查找是否有单品收藏
 *
 *  @param P_j_id 拼接id
 *
 *  @return 返回布尔值
 */
- (BOOL)findSkuWithId:(NSString *)P_j_id
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Sku"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"p_j_id = %@",P_j_id];
    request.predicate = predicate;
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    if (array.count == 0)
    {
        return NO;
    }
    else
    {
        return YES;
    }

}

/**
 *  查找每日一晒
 *
 *  @param P_j_id 拼接id
 *
 *  @return 返回布尔值
 */
- (BOOL)findEveryWithId:(NSString *)P_j_id
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Every"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"p_j_id= %@",P_j_id];
    request.predicate = predicate;
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    if (array.count == 0)
    {
        return NO;
    }
    else
    {
        return YES;
    }

}




//返回播放的数组
- (NSArray *)allPlay
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Play"];
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    return array;
}

// 返回每日一晒的数组
- (NSArray *)allEvery
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Every"];
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    return array;
}

//返回单品数组
- (NSArray *)allSku
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Sku"];
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    return array;
}

//删除播放的数组
- (void)deleteAllPlay
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Play"];
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    for (NSManagedObject *obj in array)
    {
        [self.context deleteObject:obj];
    }
    [self.context save:nil];
    }

// 删除每日一晒的数组
- (void)deleteAllEvery
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Every"];
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    for (NSManagedObject *obj in array)
    {
        [self.context deleteObject:obj];
    }
    [self.context save:nil];


}

//删除单品数组
- (void)deleteAllSku
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Sku"];
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    for (NSManagedObject *obj in array)
    {
        [self.context deleteObject:obj];
    }
    [self.context save:nil];
}


@end
